/*
 * ListenerNuevoCd.java
 *
 * Created on 28 de octubre de 2005, 1:22
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package fmap.gui.actionListener;

import fmap.DatosCompartidos;
import fmap.actions.cat.GetCatsAction;
import fmap.dao.CatDao;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import fmap.gui.JFrameAdminCat;
import fmap.model.Cat;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author javi
 */
public class ListenerJFrameAdminCat implements ActionListener,ListSelectionListener
{
    JFrameAdminCat jFrameAdminCat;
    Cat catSelect;
    
    /** Creates a new instance of ListenerNuevoCd */
    public ListenerJFrameAdminCat ( JFrameAdminCat _jFrameAdminCat )
    {
        jFrameAdminCat = _jFrameAdminCat;
    }
    
    
    
    public void valueChanged (ListSelectionEvent event)
    {
        JList listCat   = (JList)event.getSource ();
        catSelect     = (Cat)listCat.getSelectedValue ();
    }
    
    
    public void actionPerformed (ActionEvent ae)
    {
        JButton jb = (JButton)ae.getSource ();
        
        // Insertamos una nueva categoria
        if( jb.getActionCommand ().equals ("guardar") )
        {
            
            
            if( !jFrameAdminCat.getTextFCatMapF ().getText ().trim ().equals ("") )
            {
                
                new CatDao ().insertCat ( jFrameAdminCat.getTextFCatMapF ().getText () );
                
                // Actualizamos la lista de categorias
                
                Cat[] aCat = new GetCatsAction ().GetCatsAction ();
                
                if( aCat.length > 0 ) jFrameAdminCat.getListCatMapF ().setListData ( aCat );
                
                DatosCompartidos.setACat ( aCat );
                
                jFrameAdminCat.getTextFCatMapF ().setText ("");
            }
            else
            {
                /** Validamos la entrada de datos para una nueva coleccion */
                JOptionPane.showMessageDialog (jFrameAdminCat,
                        "Debe cubrir el nombre de la categoria",
                        "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
        else if ( jb.getActionCommand ().equals ("eliminar") )
        {
            if( catSelect == null )
            {
                /** Validamos la entrada de datos para una nueva coleccion */
                JOptionPane.showMessageDialog (jFrameAdminCat,
                        "Debe seleccionar la categoria a eliminar",
                        "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            }
            else
            {

                int opcion = JOptionPane.showConfirmDialog (jFrameAdminCat,
                        "¿Esta seguro de eliminar esta categoria?",
                        "¿Seguro?",
                        JOptionPane.YES_NO_OPTION);

                if( opcion != 1 )
                {
                    new CatDao ().deleteCat ( catSelect.getId () );

                    // Actualizamos la lista de categorias
                    Cat[] aCat = new GetCatsAction ().GetCatsAction ();

                    if( aCat.length > 0 ) jFrameAdminCat.getListCatMapF ().setListData ( aCat );
                    DatosCompartidos.setACat ( aCat );
                }
            }
        }
    }
}